package com.avidys.gwt.wom.client;

import com.avidys.common.gwt.net.ControlledWidget;
import com.avidys.common.gwt.net.Controller;
import com.avidys.common.gwt.net.JsonHttp;
import com.avidys.common.gwt.net.Logging;
import com.avidys.common.gwt.net.JSONRPC.ServerResult;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusWidget;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;

/*
 * A Grid to test step by step server communication
 * incorporates SID 
 */
public class SequentialTestServerPanel extends Composite implements ControlledWidget{

	String[] steps;
	String[] Results;
	
	MyHandler handler = new MyHandler();
	Button resetButton = new Button("RESET");
	ToggleButton[] toggleButtons;
	public int currentStep=-1;
	private Controller controller ;

	public SequentialTestServerPanel(String[] steps) {
		this.steps = steps;
		this.toggleButtons = new ToggleButton[steps.length];
		
		VerticalPanel vp= new VerticalPanel();

		vp.add(new Label("Test Grid"));

		Grid grid = new Grid (steps.length,5);
		
		    // You can use the CellFormatter to affect the layout of the grid's cells.
		    CellFormatter cf = grid.getCellFormatter();
		    RowFormatter rowf = grid.getRowFormatter();
		    ColumnFormatter colf = grid.getColumnFormatter();
		    // .setWidth(0, 2, "256px");

			colf.setWidth(0,"10");
			colf.setWidth(1,"30");
			colf.setWidth(2,"200");
			colf.setWidth(3,"400");
			colf.setWidth(4,"35");

			for (int i = 0; i < steps.length; i++) {
				rowf.setVerticalAlign(i, HasVerticalAlignment.ALIGN_MIDDLE);
				grid.setText(i, 0, String.valueOf(i+1));
				grid.setText(i, 1, steps[i]);

				TextBox t = new TextBox();
				t.setMaxLength(120);
				grid.setWidget(i, 2, t);

				Label la = new Label(" ");
				grid.setWidget(i, 3, la);

				ToggleButton tb = new ToggleButton(" GO ","Done"); 
//				DOM.setElementAttribute(tb.getElement(), "id", "tb-"+i);
				tb.getElement().setId("tb-"+i);
				//Element e = getElement();
				//e.setAttribute("Style", style);
//				DOM.setElementAttribute(tb.getElement(),"style", "font-size: 12pt; color: green");
				// tb.getElement().setAttribute("style", "font-size: 12pt; color: fuchsia");
				//tb.addStyleDependentName("font");
				//tb.addStyleName("button-font");
				tb.setPixelSize(35, 15);
//				tb.setHorizontalAlignement(Has.Center);
				tb.setTitle("Step "+i+"");
				tb.setTabIndex(i);
				tb.addClickHandler(handler);
				ToolTipConfig.setToolTip(tb, new ToolTipConfig("Step "+(i+1), "Press the buttons in a sequential order"));  
				grid.setWidget(i, 4, tb);
				toggleButtons[i]=tb;
			}
			grid.setBorderWidth(1);
			grid.setCellSpacing(0);
			grid.setCellPadding(4);
			vp.add(grid);
		
			HorizontalPanel hp= new HorizontalPanel();
			resetButton.setEnabled(false);
			resetButton.addClickHandler(handler);
			DOM.setElementAttribute(resetButton.getElement(), "id", "id-reset");
			resetButton.setPixelSize(40, 20);
			hp.add(resetButton);
			vp.add(hp);
		initWidget(vp);
	}

	class MyHandler implements ClickHandler {
		int ID;
		
		public MyHandler(int iD) {
			super();
			ID = iD;
		}

		public MyHandler() { super();}

		public void onClick(ClickEvent event) {
			UIObject s= (UIObject) event.getSource(); 
			//String id = DOM.getElementAttribute(s.getElement(), "id");
			String id = s.getElement().getAttribute("id");
			//if (Properties.environment==ENVIRONMENT.DEVELOPMENT)
			//	GWT.log("Click "+event.toDebugString()+" "+ id, null);
			if (id.equals("id-reset")){
				resetServer();
				for (int i = 0; i < steps.length; i++) {
					toggleButtons[i].setDown(false);
					toggleButtons[i].setEnabled(true);
					resetButton.setEnabled(false);
					currentStep=-1;
				}  
			}
			else{
				if ( ((ToggleButton) s).isDown()) {
						int idi = Integer.parseInt(id.substring(3));
						Logging.debug("ID "+currentStep+" "+ idi);
						if (idi == currentStep+1) {
							((FocusWidget) s).setEnabled(false);
							if (currentStep == -1)
								resetButton.setEnabled(true);
							currentStep++;
							Logging.debug("STEP "+currentStep, null);
							sendServer(steps[currentStep],id);
						} else
							((ToggleButton) s).setDown(false);				
					
				} else {
					((ToggleButton) s).setDown(true);				
				}
			}
		}
	}


	public void sendServer(String string, String id) {
		controller.sendTextToServer(string,null,id);
		// TODO get params
	}

	public void resetServer() {
		Logging.info("Send reset to server", null);
	}

	@Override
	public void onLogicError(String error) {
		Logging.info("Logic error");
	}

	@Override
	public void onRPCError(Throwable caught) {
		Logging.info("RPC error");
	}


	@Override
	public void setController(Controller controller) {
		this.controller = controller;
	}

	@Override
	public void onTextReceived(ServerResult[] r) {
		Logging.info("Sequential Test Received ["+r[0].toString()+"]");

	}


}
